package com.fastfood.model.blo;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.fastfood.model.dao.AccountDAO;
import com.fastfood.model.dto.Account;
import com.fastfood.util.DBConnectionPool;

public class AccountBLO {

	private AccountDAO dao = new AccountDAO();
	
	public List<Account> getAllAccounts() {
		List<Account> accounts = null;
		Connection conn = DBConnectionPool.getConnectionPool().getConnection();
		if (conn != null) {
			accounts = dao.getAllAccounts(conn);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return accounts;
	}
	
	public boolean addAccount(Account account) {
		boolean result = false;
		Connection conn = DBConnectionPool.getConnectionPool().getConnection();
		if (conn != null) {
			result = dao.addAccount(conn, account);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public boolean updateAccount(Account account) {
		boolean result = false;
		Connection conn = DBConnectionPool.getConnectionPool().getConnection();
		if (conn != null) {
			result = dao.updateAccount(conn, account);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public boolean deleteAccount(Account account) {
		boolean result = false;
		Connection conn = DBConnectionPool.getConnectionPool().getConnection();
		if (conn != null) {
			result = dao.deleteAccount(conn, account);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	public Account getAccountByUsernameAndPassword(String username, String password) {
		Account account = null;
		Connection conn = DBConnectionPool.getConnectionPool().getConnection();
		if (conn != null) {
			account = dao.getAccountByUsernameAndPassword(conn, username, password);
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return account;
	}
}
